package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsCategory;
import com.qf.service.AdminCategoryService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RequestMapping("/admin/category")
@CrossOrigin
@RestController
public class AdminCategoryController {

    @Autowired
    private AdminCategoryService adminCategoryService;


    /**
     * 查询商品类目列表
     * @param page
     * @param limit
     * @param id
     * @param name
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:category:list")
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "商品类目列表")
    public Object categoryList(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "20") Integer limit,
                               Integer id,String name,
                               String sort,String order){
        IPage<DtsCategory> iPage = adminCategoryService.selectCategoryList(page,limit,id,name,sort,order);

        long total = iPage.getTotal();
        List<DtsCategory> records = iPage.getRecords();

        DtsCategory[] items = new DtsCategory[records.size()];
        for (int i = 0; i < records.size(); i++) {
            items[i] = records.get(i);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);

        return ResponseUtil.ok(data);
    }

    //商品一级类目列表
    @GetMapping("/l1")
    @RequiresPermissions("admin:category:l1")
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "商品一级类目列表")
    public Object categoryl1(){

        List<DtsCategory> dtsCategory = adminCategoryService.selectCategoryL1();

        HashMap[] data = new HashMap[dtsCategory.size()];

        for (int i = 0; i < dtsCategory.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("value", dtsCategory.get(i).getId());
            map.put("label", dtsCategory.get(i).getName());
            data[i] = map;
        }

        return ResponseUtil.ok(data);
    }

    //商品类目添加
    @PostMapping("/create")
    @RequiresPermissions("admin:category:create")
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "商品类目添加")
    public Object categoryCreate(@RequestBody DtsCategory dtsCategory){

        DtsCategory data = adminCategoryService.insertCategory(dtsCategory);

        return ResponseUtil.ok(data);
    }

    //商品类目修改
    @PostMapping("/update")
    @RequiresPermissions("admin:category:update")
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "商品类目修改")
    public Object categoryUpdate(@RequestBody DtsCategory dtsCategory){
        adminCategoryService.updateCategory(dtsCategory);
        return ResponseUtil.ok();
    }

    @PostMapping("/delete")
    @RequiresPermissions("admin:category:delete")
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "商品类目删除")
    public Object categoryDelete(@RequestBody DtsCategory dtsCategory){
        adminCategoryService.deleteCategory(dtsCategory.getId());
        return ResponseUtil.ok();
    }

}
